package main

import (
	"time"
)

/*
// -lm 链接math库
#cgo LDFLAGS: -lm
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <math.h>

// randomDouble 获取start end之间的一个随机浮点数，执行前必须先初始化随机数种子
double randomDouble(double start, double end){
	double dis = end - start;
    return rand() / (double)RAND_MAX * dis + start;
}

// createMockDataBatch 生成一批模拟数据
void createMockDataBatch(double buffer[], int batchCount) {
	static int t = 0;
	double value = 0;
	for (int i = 0; i < batchCount; ++i) {
		// 三角函数模拟
		value = sin(t * 0.01);
		t++;
		if (t > 9999999999) {
			t = 0;
		}

		// 随机数模拟
		// value = value + randomDouble(-5, 5);
		// if (value > 50 || value < -50) {
			// 防止随机value跑太偏
		//	value = 0;
		// }
		buffer[i] = value;
	}
}

// createMockDataInit 创建存储模拟数据的buffer，并初始化随机数种子，bufferPointer 指向buffer（buffer是指针）的指针
void createMockDataInit(double **bufferPointer, int len) {
	double *buffer = (double *)malloc(len * sizeof(double));
    if (buffer == NULL) {
        puts("failed to allocate space for buffer");
        exit(-1);
	}
	*bufferPointer = buffer;
	srand((unsigned)time(0));
}
*/
import "C"

func mockData() {
	samplesPer100Milliseconds := conf.SamplesPerSecond / 10
	var buffer *C.double
	C.createMockDataInit(&buffer, C.int(samplesPer100Milliseconds))

	// 每100ms生成一批模拟数据
	ticker := time.NewTicker(100 * time.Millisecond)
	for range ticker.C {
		C.createMockDataBatch(buffer, C.int(samplesPer100Milliseconds))
		handleData(buffer, C.int(samplesPer100Milliseconds))
	}
}

// TODO：
// 如果生成数据耗时过长，1s内没办法执行10次生成，则每秒生成的数目不足
// 如何补足？或者能否判断实在不能补足，提示生成能力不足？
// 可能有用的C函数：clock()
